PRO FITSX
; Generate IBIS Mask Pattern
M=GEN_IBIS_MASK()

G=M+M-1
G[47,47]=1

; Interpolation

G1=IP(G)

M1=IP(M)

;Read Fits

D=READFITS('/workroom2/odyssey/Integral/vela/obs/008600440140/scw/008600440140.001/isgri_cor_shad.fits',EXTEN_NO = 2)

; Generate the Dirty Image

S1=CONV(D,G1)

; Correction

M10=M1[51:180,49:182]
Q=MAX(CONV(M10,G1))

M0=FLTARR(130,134)+1.0
S0=CONV(M0,G1)

FOR i=0,364 DO BEGIN
 FOR j=0,360 DO BEGIN
  IF (S0[j,i] LT 50) && (S0[j,i] GT 30) THEN BEGIN
     S0[j,i]=10*S0[j,i]
  ENDIF ELSE IF (S0[j,i] LT 30) && (S0[j,i] GT 1) THEN BEGIN
     S0[j,i]=50*S0[j,i]
  ENDIF ELSE IF S0[j,i] LE 1 THEN BEGIN
     S0[j,i]=S1[j,i]/10
  ENDIF
 ENDFOR
ENDFOR

; Initialization of CLEAN

S1=S1/S0
CLEAN=FLTARR(361,365)
CC=0L
REPEAT BEGIN
P=S1[0,0]
U=0
V=0
DIF=FLTARR(361,365)

; Find the Brightest Point 

FOR i=0,364 DO BEGIN
 FOR j=0,360 DO BEGIN
  IF S1[j,i] GT P THEN BEGIN
    P=S1[j,i]
    U=i
    V=j
  ENDIF 
 ENDFOR
ENDFOR
DIF[V,U]=0.01*P

; DifCLEAN

IF V-64 GE 0 && U-66 GE 0 && V-64 LE 232 && U-66 LE 232 THEN BEGIN
 SC=FLTARR(233,233)
 SC[V-64,U-66]=0.01*P/Q
 DC0=CONV(SC,M1)
 DC=DC0[167:296,165:298]
 S1C=CONV(DC,G1)
 S1=S1-S1C/S0
 CLEAN=CLEAN+DIF
ENDIF ELSE BEGIN
 S1=S1-10*DIF
ENDELSE

CC=CC+1
ENDREP UNTIL CC GT 1400
S1=CLEAN+S1

;S2=FLTARR(129,129)
;S2[0:128,0:128]=S1[116:244,118:246]

; Generate the Clean Image

;WINDOW,0,Xsize=129,Ysize=129
;device, decompose=0
;loadct, 3
;TVSCL,S2
;ZOOM,FACT=3,XSIZE=387,YSIZE=387

WINDOW,0,Xsize=742,Ysize=730
device, decompose=0
loadct, 3
AXIS,0,3000,COLOR='0000FF'XL,/T3D,Charsize=1,Xaxis=1
ncolors=!D.Table_Size
colorbar=Replicate(1B,20)#LIndGen(560)
X=!D.X_VSIZE
Y=!D.Y_VSIZE
TVSCL,Congrid(S1,560,560),150,110
TV,BytScl(colorbar,Top=ncolors-2),130,110

;WRITE_IMAGE,'CLEAN.png','png',S1

END
